<?php
// Account module
class ProfileController extends Controller {
    
    private $_model;
    
    public function init(){
        parent::init();
        
        $this->breadcrumbs=Yii::app()->user->isMerchant?
                array(Lang::t('merchant.page.breadcrumbs')=>Yii::app()->user->returnUrl):
                array(Lang::t('user.page.breadcrumbs')=>'');
    }
    
    public function actionIndex(){
        $this->render('index',array(
            
        ));
    }
    
    public function actionView(){
        $model=$this->loadModel();
        $this->render('view', array(
            'data'=>$model,
        ));
    }
    
    public function actionUpdate(){
        $model = $this->loadModel();
        $model->scenario='userUpdate';
        $model->autoFill();
        
        $address=empty($model->address)?new Address:$model->address;
        $address->autoFill($model);
        $address->validatorList->add(CValidator::createValidator('required', $address, 'address_'));
        
        $content=empty($model->html)?new Content('adminCreate'):$model->html;
        $content->autoFill($model);
        //$content->validatorList->add(CValidator::createValidator('required', $content, 'content_'));
        
        $this->performAjaxValidation($model, $address, $content);
        
        $params=Yii::app()->request->getParam(get_class($model));
        $addressParams=Yii::app()->request->getParam(get_class($address));
        $contentParams=Yii::app()->request->getParam(get_class($content));
        if ($params && $addressParams && $contentParams) {
            $model->attributes = $params;
            $model->image_= CUploadedFile::getInstances($model, 'image_');
            $model->validate();
            
            $address->attributes=$addressParams;
            $address->validate();
            
            $content->attributes=$contentParams;
            $content->title_=$model->name_;
            $content->validate();
            
            if (!$model->hasErrors() && !$address->hasErrors() && !$content->hasErrors() && $model->updateRecord($address, $content)) {
                $model->refresh();
                $model->updateJson();
                $this->setFlash('success', Lang::t('account.update.flash.success'));
                $this->refresh();
            }else{
               $this->setFlash('error', Lang::t('account.update.flash.fail'));
               //if($this->debugMode) print_r($model->errors);
            }
        }

        $this->render('updateForm', array(
            'account' => $model,
            'address'=>$address,
            'content'=>$content,
            'formConfig' => array(
                'id' => $this->formId,
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'htmlOptions' => array(
                    'enctype' => 'multipart/form-data',
                ),
                'focus' => array($model, 'name_'),
            ),
        ));
    }
    
    public function loadModel() {
        if ($this->_model === null) {
            
            if(Yii::app()->user->isMerchant){
                $this->_model = Account::model()->accessible()->findByPk((int)Yii::app()->user->accountId);
            }else{
                $id=Yii::app()->request->params('accountId');
                if(empty($id)) HttpStatus::code(404);
                $this->_model = Account::model()->publish()->findByPk((int)$id);
            }
            if ($this->_model === null)
                HttpStatus::code(404);
        }
        return $this->_model;
    }
}